

interface IuiNavScope extends ng.IScope {
        jfry_external_order:boolean; 
        jfry_external_inquiry :boolean;
        jfry_external_delivery :boolean;
        jfry_client :boolean;
        jfry_client_manage :boolean;
        jfry_inquiry :boolean;
        jfry_inquiry_add :boolean;
        jfry_inquiry_accept :boolean;
        jfry_inquiry_receivable :boolean;
        jfry_inquiry_audit :boolean;
        jfry_inquiry_manage :boolean;
        jfry_order:boolean;
        jfry_order_add :boolean;
        jfry_order_accept :boolean;
        jfry_order_audit :boolean;
        jfry_order_register :boolean;
        jfry_order_manage :boolean;
        jfry_order_admin:boolean;
        jfry_cars:boolean;
        jfry_cars_manage :boolean;
        jfry_carrier_manage :boolean;
        jfry_fee :boolean;
        jfry_receipt_admin:boolean; 
        jfry_fee_audit :boolean;
        jfry_cost_recoverable :boolean;
        jfry_settle_admin :boolean;
        jfry_payment_admin:boolean;
        jfry_payable_admin :boolean;
        jfry_receivable :boolean;
        jfry_receivable_settle :boolean;
        jfry_receivable_bill :boolean;
        jfry_receivable_admin :boolean;
        jfry_receivable_census:boolean;
        jfry_receivable_accountreturnanalysis:boolean;
        jfry_excel :boolean;
        jfry_fee_excel :boolean;
        jfry_receivable_excel :boolean;
        jfry_profit_excel :boolean;
        jfry_custom_excel:boolean;
        jfry_capacity_excel:boolean;
        jfry_business_excel :boolean;
        jfry_dispatch_profit:boolean;
        jfry_set_up:boolean;
        jfry_authorization_type:boolean;
        jfry_authorization :boolean;
        jfry_department_manage:boolean;
        jfry_employee_manage :boolean;
        jfry_user_manage :boolean;
        jfry_usergroup_manage:boolean;
        jfry_logistics_manage :boolean;
        jfry_report :boolean;
        jfry_report_export :boolean;
        jfry_report_workreport:boolean;
        jfry_operation:boolean;
        jfry_operation_list:boolean;
        jfry_opinion__manage:boolean;
        jfry_location_list:boolean;
}

class uiNav implements ng.IDirective {
  constructor() { }
  static instance() : ng.IDirective { return new uiNav; }
  templateUrl: string = './src/directives/ui-nav.html';
  restrict: string = 'EA';
  link($scope : IuiNavScope, elements:ng.IAugmentedJQuery, attrs : ng.IAttributes) {

  var _window=$(window),
           _mb = 768,
                  wrap = $('.app-aside'),
                  next:any,
                  backdrop = '.dropdown-backdrop';
                  // unfolded
                  elements.on('click', 'a', function (e) {
                      next && next.trigger('mouseleave.nav');
                      var _this = $(this);
                      _this.parent().siblings(".active").toggleClass('active');
                      _this.next().is('ul') && _this.parent().toggleClass('active') && e.preventDefault();
                      // mobile
                      _this.next().is('ul') || ((_window.width() < _mb) && $('.app-aside').removeClass('show off-screen'));
                  });

                  // folded & fixed
                  elements.on('mouseenter', 'a', function (e) {
                      next && next.trigger('mouseleave.nav');
                      $('> .nav', wrap).remove();
                      if (!$('.app-aside-fixed.app-aside-folded').length || (_window.width() < _mb) || $('.app-aside-dock').length) return;
                      var _this = $(e.target)
                      , top
                      , w_h = $(window).height()
                      , offset = 50
                      , min = 150;

                      !_this.is('a') && (_this = _this.closest('a'));
                      if (_this.next().is('ul')) {
                          next = _this.next();
                      } else {
                          return;
                      }

                      _this.parent().addClass('active');
                      top = _this.parent().position().top + offset;
                      next.css('top', top);
                      if (top + next.height() > w_h) {
                          next.css('bottom', 0);
                      }
                      if (top + min > w_h) {
                          next.css('bottom', w_h - top - offset).css('top', 'auto');
                      }
                      next.appendTo(wrap);

                      next.on('mouseleave.nav', function (e) {
                          $(backdrop).remove();
                          next.appendTo(_this.parent());
                          next.off('mouseleave.nav').css('top', 'auto').css('bottom', 'auto');
                          _this.parent().removeClass('active');
                      });

                      $('.smart').length && $('<div class="dropdown-backdrop"/>').insertAfter('.app-aside').on('click', function (next) {
                          next && next['trigger']('mouseleave.nav'); 
                      });

                  });

                  wrap.on('mouseleave', function (e) {
                      next && next.trigger('mouseleave.nav');
                      $('> .nav', wrap).remove();
                  });
       
  }
}
angular.module('tms').directive('uiNav', uiNav.instance);